/***********************************************************************************************************************
    @file    bsp_tim1_pwm_output.c
    @author  FAE Team
    @date    25-May-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/

/* Define to prevent recursive inclusion */
#define _TIM1_PWM_OUTPUT_C_

/* Files include */
#include <stdio.h>
#include "bsp_tim1_pwm_output.h"

//#define FREQ_DIV 100000 //100KHZ
#define FREQ_DIV 100    //100HZ

/**
  * @addtogroup MM32F0140_LibSamples
  * @{
  */

/**
  * @addtogroup TIM1
  * @{
  */

/**
  * @addtogroup TIM1_PWM_Output
  * @{
  */

/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/

/* Private macro ******************************************************************************************************/

/* Private variables **************************************************************************************************/

/* Private functions **************************************************************************************************/

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void BSP_TIM1_Configure(void)
{
    GPIO_InitTypeDef        GPIO_InitStruct;
    TIM_OCInitTypeDef       TIM_OCInitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;

    RCC_ClocksTypeDef RCC_Clocks;

    RCC_GetClocksFreq(&RCC_Clocks);

    /* Compute the value to be set in ARR regiter to generate signal frequency at 100 Khz */
    uint32_t TimerPeriod = (RCC_Clocks.PCLK2_Frequency / FREQ_DIV ) - 1;

    /*zhao 
     *remember brightness is reversed order to ChannelnPulse.
     */
    
//    /* Compute CCR1 value to generate a duty cycle at 80% for channel 1 */
//    uint32_t Channel1Pulse = ((uint32_t)800 * (TimerPeriod - 1)) / 1000;

//    /* Compute CCR2 value to generate a duty cycle at 40% for channel 2 */
//    uint32_t Channel2Pulse = ((uint32_t)400 * (TimerPeriod - 1)) / 1000;

    /* Compute CCR3 value to generate a duty cycle at 25% for channel 3 */
    uint32_t Channel3Pulse = ((uint32_t)1000 * (TimerPeriod - 1)) / 1000;

    RCC_APB2PeriphClockCmd(HEATER_EN_RCC_APB2ENR_TIM, ENABLE);

    TIM_TimeBaseStructInit(&TIM_TimeBaseStruct);
    TIM_TimeBaseStruct.TIM_Prescaler         = 0;
    TIM_TimeBaseStruct.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseStruct.TIM_Period            = TimerPeriod;
    TIM_TimeBaseStruct.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(HEATER_EN_TIM, &TIM_TimeBaseStruct);

    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode       = HEATER_EN_TIM_OCMode_PWM;
    TIM_OCInitStruct.TIM_OutputState  = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse        = 0;
    TIM_OCInitStruct.TIM_OCPolarity   = TIM_OCPolarity_High;
    TIM_OCInitStruct.TIM_OCIdleState  = TIM_OCIdleState_Set;

//    TIM_OCInitStruct.TIM_Pulse = Channel1Pulse;
//    TIM_OC1Init(HEATER_EN_TIM, &TIM_OCInitStruct);

//    TIM_OCInitStruct.TIM_Pulse = Channel2Pulse;
//    TIM_OC2Init(HEATER_EN_TIM, &TIM_OCInitStruct);

    TIM_OCInitStruct.TIM_Pulse = Channel3Pulse;
    TIM_OC3Init(HEATER_EN_TIM, &TIM_OCInitStruct);

    RCC_AHBPeriphClockCmd(HEATER_EN_RCC_AHBENR, ENABLE);

//    GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_6);    /* TIM1_CH1 */
//    GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_6);    /* TIM1_CH2 */
    GPIO_PinAFConfig(HEATER_EN_PORT, HEATER_EN_PIN_SOURCE, HEATER_EN_PIN_ALT);    /* TIM1_CH3 */

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = HEATER_EN_PIN/* | GPIO_Pin_4 | GPIO_Pin_5*/;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = HEATER_EN_MODE;
    GPIO_Init(HEATER_EN_PORT, &GPIO_InitStruct);
}

/*******************************************************************************
 * @Name      : BSP_TIM1_Set_PWM_Level_int
 * @Purpose   : BSP_TIM1_Set_PWM_Level_int
 * @Param     : level (0~100)
 * @Retval    : None
 *******************************************************************************/
void BSP_TIM1_Set_PWM_Level_int(uint8_t level)
{
    GPIO_InitTypeDef        GPIO_InitStruct;
    TIM_OCInitTypeDef       TIM_OCInitStruct;
//    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;

    RCC_ClocksTypeDef RCC_Clocks;

    RCC_GetClocksFreq(&RCC_Clocks);

    /* Compute the value to be set in ARR regiter to generate signal frequency at 100 hz */
    uint32_t TimerPeriod = (RCC_Clocks.PCLK2_Frequency / FREQ_DIV ) - 1;
    
    if(level > 100)
    {
        level = 100;
    }

    //level = 100 - level;

    /*zhao 
     *remember brightness is reversed order to ChannelnPulse.
     */
    
//    /* Compute CCR1 value to generate a duty cycle at 80% for channel 1 */
//    uint32_t Channel1Pulse = ((uint32_t)800 * (TimerPeriod - 1)) / 1000;

//    /* Compute CCR2 value to generate a duty cycle at 40% for channel 2 */
//    uint32_t Channel2Pulse = ((uint32_t)400 * (TimerPeriod - 1)) / 1000;

    /* Compute CCR3 value to generate a duty cycle at 25% for channel 3 */
    uint32_t Channel3Pulse = ((uint32_t)level * 10 * (TimerPeriod - 1)) / 1000;

    RCC_APB2PeriphClockCmd(HEATER_EN_RCC_APB2ENR_TIM, ENABLE);

//    TIM_TimeBaseStructInit(&TIM_TimeBaseStruct);
//    TIM_TimeBaseStruct.TIM_Prescaler         = 0;
//    TIM_TimeBaseStruct.TIM_CounterMode       = TIM_CounterMode_Up;
//    TIM_TimeBaseStruct.TIM_Period            = TimerPeriod;
//    TIM_TimeBaseStruct.TIM_ClockDivision     = TIM_CKD_DIV1;
//    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
//    TIM_TimeBaseInit(HEATER_EN_TIM, &TIM_TimeBaseStruct);

    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode       = HEATER_EN_TIM_OCMode_PWM;
    TIM_OCInitStruct.TIM_OutputState  = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse        = 0;
    TIM_OCInitStruct.TIM_OCPolarity   = TIM_OCPolarity_High;
    TIM_OCInitStruct.TIM_OCIdleState  = TIM_OCIdleState_Set;

//    TIM_OCInitStruct.TIM_Pulse = Channel1Pulse;
//    TIM_OC1Init(HEATER_EN_TIM, &TIM_OCInitStruct);

//    TIM_OCInitStruct.TIM_Pulse = Channel2Pulse;
//    TIM_OC2Init(HEATER_EN_TIM, &TIM_OCInitStruct);

    TIM_OCInitStruct.TIM_Pulse = Channel3Pulse;
    TIM_OC3Init(HEATER_EN_TIM, &TIM_OCInitStruct);

    RCC_AHBPeriphClockCmd(HEATER_EN_RCC_AHBENR, ENABLE);

//    GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_6);    /* TIM1_CH1 */
//    GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_6);    /* TIM1_CH2 */
    GPIO_PinAFConfig(HEATER_EN_PORT, HEATER_EN_PIN_SOURCE, HEATER_EN_PIN_ALT);    /* TIM1_CH3 */

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = HEATER_EN_PIN/*GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5*/;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = HEATER_EN_MODE;
    GPIO_Init(HEATER_EN_PORT, &GPIO_InitStruct);

    TIM_Cmd(HEATER_EN_TIM, ENABLE);
    TIM_CtrlPWMOutputs(HEATER_EN_TIM, ENABLE);
}

void BSP_TIM1_Set_PWM_Level(uint8_t level)
{
    if(level == 0)
    {
        BSP_GPIO_TurnOff_Heater();
    }
    else
    {
        BSP_TIM1_Configure();
        BSP_TIM1_Set_PWM_Level_int(level);
    }
}
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/********************************************** (C) Copyright MindMotion **********************************************/

